package com.hownoon;

import com.opencsv.CSVWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class A {
    public void exec(String startTime, String stopTime,String tableName,ArrayList<String> codeArrayList,ArrayList<String> fieldArrayList) throws InterruptedException, IOException {
        int mysize=0;

        fileWriter=new FileWriter("F:\\test\\out.csv");
        csvWriter=new CSVWriter(fileWriter);


        Collections.sort(codeArrayList);
        Collections.sort(fieldArrayList);


        int size1=fieldArrayList.size();
        String[] array1 = (String[])fieldArrayList.toArray(new String[size1]);

        csvWriter.writeNext(array1);
        csvWriter.flush();

        System.out.println("准备完毕");

        long time1 =  System.currentTimeMillis();


        ArrayList<ArrayList<ArrayList<String>>> bigArrayList=getBigArrayList(startTime,stopTime,tableName,codeArrayList,fieldArrayList);

        long time2 =  System.currentTimeMillis();
        long usedTime = time2-time1;
        System.out.println("总查询耗时为<====>"+usedTime+"毫秒");

        System.out.println("大小<===>"+bigArrayList.size());
        System.out.println("已获取全部股票数据");

        int sum=bigArrayList.get(0).size()-29;

        long time3 =  System.currentTimeMillis();

        for(int i=0;i<sum;i++){
            for(int z=0;z<bigArrayList.size();z++){

                ArrayList<String[]> arrayArraylist=new ArrayList<>();
                boolean isWrite=true;

                for(int j=i;j<30+i;j++){
                    ArrayList<String> arrayList1= bigArrayList.get(z).get(j);
                    if(isNull(arrayList1)){
                        isWrite=false;
                        break;
                    }
                    int size=arrayList1.size();
                    String[] array = (String[])arrayList1.toArray(new String[size]);
                    arrayArraylist.add(array);
                }

                if(isWrite){
                    write(arrayArraylist);
                    mysize=mysize+30;
                }

            }

            System.out.println("写入次数进度<==>"+(i+1)+"/"+sum);
        }

        long time4 =  System.currentTimeMillis();
        long usedTime1 = time4-time3;

        System.out.println("写入总个数<====>"+mysize);
        System.out.println("写入总耗时为<====>"+usedTime1+"毫秒");

    }
	
    public ArrayList<ArrayList<ArrayList<String>>> getBigArrayList(String startTime, String stopTime,String tableName,ArrayList<String> codeArrayList,ArrayList<String> fieldArrayList){
        ArrayList<ArrayList<ArrayList<String>>> bigArrayList=new ArrayList<>();
        int size=codeArrayList.size();
        for(int i=0;i<codeArrayList.size();i++){
            String code=codeArrayList.get(i);
            String first[]=code.split("\\.");
            int second = Integer.parseInt(first[0]);

            String part;
            if (second % 50 < 10) {
                part = "0" + second % 50;
            } else {
                part = Long.toString(second % 50);
            }
            String startRow=part+"_"+code+"_"+startTime;
            String stopRow=part+"_"+code+"_"+stopTime+"|";


            ArrayList<ArrayList<String>> arrayList=getDataScan(tableName,connection,startRow,stopRow,fieldArrayList);

            if(arrayList.size()!=0){
                bigArrayList.add(arrayList);
            }
            System.out.println("加载股票进度<==>"+(i+1)+"/"+size);
        }
        return bigArrayList;
    }
}
